import socket

# 1创建套接字
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 2绑定地址
sockfd.bind(('0.0.0.0', 8080))

# 3设置监听
sockfd.listen(200)

# 4等待处理客户端连接
while True:

    print('Waiting for connect.....')
    try:
        connfd, addr = sockfd.accept()
        print('connect from :', addr)
    except KeyboardInterrupt:
        print('退出服务')
        break

    while True:
        # 5接受消息
        data = connfd.recv(1024)
        #  得到空则表示退出
        if not data:
            break
        print('接收到的消息：', data.decode())

        # 6发送消息
        n = connfd.send(b'Receive your message')
        print('发送了%d个字节数据' % n)

    # 7关闭套接字
    connfd.close()

sockfd.close()
